/****************************************Copyright (c)****************************************************
 
**--------------File Info---------------------------------------------------------------------------------
** File name:                button.h
** Last modified Date:       2024.08.14
** Last Version:             Ver:1.0
** Descriptions:             基于状态机的多功能按键处理模块
**                        
**--------------------------------------------------------------------------------------------------------
** Created by:               adm
** Created date:             2024.08.14
** Version:                  Ver:1.0
** Descriptions:             基于状态机的多功能按键处理模块
**                           
**--------------------------------------------------------------------------------------------------------*/
/*
Copyright (c) [2024]     [BA6IV  ]   [SINEWAVE] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan
PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#ifndef _BUTTON_H_
#define _BUTTON_H_

#include "stdint.h"
#include "string.h"

//根据应用需要定义的常量
#define TICKS_INTERVAL    10	                    //ms
#define DEBOUNCE_TICKS    5                         //MAX 8
#define SHORT_TICKS       (100  /TICKS_INTERVAL)	//单击时间设置为100ms，提高按键灵敏度
#define LONG_TICKS        (1200 /TICKS_INTERVAL)    //长按时间设置为1200ms


//回调函数指针类型
typedef void (*BtnCallback)(void*);

//按键状态枚举
typedef enum {
	PRESS_DOWN = 0,
	PRESS_UP,
	PRESS_REPEAT,
	SINGLE_CLICK,
	DOUBLE_CLICK,
	LONG_PRESS_START,
	LONG_PRESS_HOLD,
	number_of_event,
	NONE_PRESS
}PressEvent;

//按键类型结构体
typedef struct Button {
	uint16_t ticks;
	uint8_t  repeat       : 4;
	uint8_t  event        : 4;
	uint8_t  state        : 3;
	uint8_t  debounce_cnt : 3; 
	uint8_t  active_level : 1;
	uint8_t  button_level : 1;
	uint8_t  (*hal_button_Level)(void);
	BtnCallback  cb[number_of_event];
	struct Button* next;
}Button;

#ifdef __cplusplus  
extern "C" {  
#endif  

//按键模块接口函数
void button_init(struct Button* handle, uint8_t(*pin_level)(), uint8_t active_level);
void button_attach(struct Button* handle, PressEvent event, BtnCallback cb);
PressEvent get_button_event(struct Button* handle);
int  button_start(struct Button* handle);
void button_stop(struct Button* handle);
void button_ticks(void);

#ifdef __cplusplus
} 
#endif

#endif
